/*
 * Copyright (C) 2015 tyrantgit
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.itheima.shotplane.util;

import com.itheima.shotplane.dialog.GameDialog;
import com.itheima.shotplane.dialog.GameDialog.OnRestartClickListener;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.ImageView;

public class Utils {

	private Utils() {
	}

	private static final float DENSITY = Resources.getSystem()
			.getDisplayMetrics().density;
	private static final Canvas sCanvas = new Canvas();

	public static int dp2Px(int dp) {
		return Math.round(dp * DENSITY);
	}

	/**
	 * 获取相对于TranslationX，y的矩形区域
	 * 
	 * @param view
	 * @return
	 */
	public static RectF getTranslationRect(View v) {
		RectF rect = new RectF(v.getTranslationX(), v.getTranslationY(),
				v.getTranslationX() + v.getWidth(), v.getTranslationY()
						+ v.getHeight());
		return rect;
	}

	public static Bitmap createBitmapFromView(View view) {
		if (view instanceof ImageView) {
			Drawable drawable = ((ImageView) view).getDrawable();
			if (drawable != null && drawable instanceof BitmapDrawable) {
				return ((BitmapDrawable) drawable).getBitmap();
			}
		}
		view.clearFocus();
		Bitmap bitmap = createBitmapSafely(view.getWidth(), view.getHeight(),
				Bitmap.Config.ARGB_8888, 1);
		if (bitmap != null) {
			synchronized (sCanvas) {
				Canvas canvas = sCanvas;
				canvas.setBitmap(bitmap);
				view.draw(canvas);
				canvas.setBitmap(null);
			}
		}
		return bitmap;
	}

	public static Bitmap createBitmapSafely(int width, int height,
			Bitmap.Config config, int retryCount) {
		try {
			return Bitmap.createBitmap(width, height, config);
		} catch (OutOfMemoryError e) {
			e.printStackTrace();
			if (retryCount > 0) {
				System.gc();
				return createBitmapSafely(width, height, config, retryCount - 1);
			}
			return null;
		}
	}

	private static GameDialog gameDialog = null;

	public static void showGameDialog(Context context,
			OnRestartClickListener listener) {
		gameDialog = new GameDialog(context);
		gameDialog.setOnRestartClickListener(listener);
		gameDialog.show();
	}
}
